//
//  CinemaListViewController.m
//  UI-豆瓣项目APP
//
//  Created by dllo on 15/9/8.
//  Copyright (c) 2015年 孙鹏. All rights reserved.
//

#import "CinemaListViewController.h"
#import "CinemaListTableViewCell.h"
#import "MJRefresh.h"
#import "AFNetworking.h"
#import "Cinema.h"
@interface CinemaListViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation CinemaListViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = @"影院";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:UIBarMetricsDefault];
    
    self.allFriendsArray = [NSMutableArray array];
    self.cinemaTableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.cinemaTableView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.cinemaTableView];
    [_cinemaTableView release];
    self.cinemaTableView.delegate = self;
    self.cinemaTableView.dataSource = self;
    [self.cinemaTableView registerClass:[CinemaListTableViewCell class] forCellReuseIdentifier:@"cinema"];
    
    self.isUpLoading = NO;

    [self addHeadRefresh];
    [self addFootRefresh];
}

- (void)addHeadRefresh{
    __block CinemaListViewController *vc = self;//因为block块里用self
    [self.cinemaTableView addHeaderWithCallback:^{
        NSLog(@"ssss");
        vc.nextPage = 1;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc GetPersonList:vc.nextPage];//重新请求数据
    }];
    [self.cinemaTableView headerBeginRefreshing];
}
-(void)addFootRefresh{
    __unsafe_unretained CinemaListViewController *vc = self;
    // 添加上拉刷新尾部控件
    [self.cinemaTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.nextPage ++;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc GetPersonList:vc.nextPage];//请求数据
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allFriendsArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cinema";
    CinemaListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell == nil){
        cell = [[CinemaListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    Cinema *c = [self.allFriendsArray objectAtIndex:indexPath.row];
    cell.movieName.text = c.personName;
    NSLog(@"person == %@",cell.movieName);
    cell.movieAddress.text = c.title;
    cell.moviePhone.text = @"0411-58185133";
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 190;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)GetPersonList:(NSInteger)nextCursor
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = [NSString stringWithFormat:@"http://watch-cdn.idailywatch.com/api/list/video/zh-hans?page=%ld&ver=iphone&app_ver=9",nextCursor];
    NSLog(@" page = %ld\n url = %@", self.nextPage, url_string);
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        NSMutableArray *allArray = responseObject;
        //        NSLog(@"使用AFN进行get请求 ===  %@",allArray);
        
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.allFriendsArray removeAllObjects];
        }
        
        for (NSDictionary *dic  in allArray) {
            
            Cinema *cinema = [[Cinema alloc] init];
            NSString *string = [NSString stringWithFormat:@"%@",[dic objectForKey:@"guid"]];
            cinema.personName = string;
            cinema.title = [dic objectForKey:@"title"];
            [self.allFriendsArray addObject:cinema];
        }
        
        [self.cinemaTableView footerEndRefreshing];
        [self.cinemaTableView headerEndRefreshing];
        [self.cinemaTableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
